/*
 * Copyright 2004-2005 companyname
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）* 复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.companyname.cn/licenses
 */

package com.companyname.common.logs.dao;

import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import com.companyname.common.base.dao.BaseDAOHibernate;

/**
 * <p>Title: 操作日志</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: companyname</p>
 * @ $Author: author $
 * @ $Date: 2005/02/16 08:27:39 $
 * @ $Revision: 1.17 $
 * @ created in 2005-11-14
 *
 */

public class OperatingLogDAOimp extends BaseDAOHibernate
                                implements OperatingLogDAO
{
        static Logger logger = Logger.getLogger(OperatingLogDAOimp.class);

        /** 获取所有模块 */
        public List findModules()
        {
                String hql = " select distinct new com.companyname.common.logs.model.Module( olog.moduleId,olog.module) " +
                             " from OperatingLog as olog order by olog.module asc";
                return this.getHibernateTemplate().find(hql);
        }

        /** 获取所有实体 */
        public List findModels()
        {
                String hql = " select distinct new com.companyname.common.logs.model.Model(olog.modelId,olog.model,olog.moduleId,olog.module) " +
                             " from OperatingLog as olog order by olog.model asc ";
                return this.getHibernateTemplate().find(hql);
        }

        /** 获取所有服务 */
        public List findServices()
        {
                String hql = " select distinct new com.companyname.common.logs.model.Service(olog.serviceId,olog.service,olog.modelId,olog.model) " +
                             " from OperatingLog as olog order by olog.service asc ";
                return this.getHibernateTemplate().find(hql);
        }

        /**
         * 描述:查询操作日志s
         *
         * @ param:
         * @ param:
         * @ param:
         * @ Exception:
         * @ return model集合
         */
        public List findOperatingLogs(String moduleId,
                                          String modelId,
                                          String serviceId,
                                          String result,
                                          String operatorName,
                                          String detail,
                                          Timestamp startTime,
                                          Timestamp endTime,
                                          String orderBy,
                                          String sortType)
        {
                String hql = " from OperatingLog " +
                             " where( " +
                             "     moduleId like ?  " +
                             " and modelId like ? " +
                             " and serviceId like ?  " +
                             " and result like ? " +
                             " and operatorName like ? " +
                             " and detail like ? " +
                             " and operatingTime >= ? " +
                             " and operatingTime <= ? " +
                             " ) " +
                             " order by " + orderBy + " " + sortType + ",operatingTime desc";
                Object[] values = new Object[8];
                values[0] = moduleId;
                values[1] = modelId;
                values[2] = serviceId;
                values[3] = result;
                values[4] = operatorName;
                values[5] = "%" + detail + "%";
                values[6] = startTime;
                values[7] = endTime;
                return this.iterator2List(this.getHibernateTemplate().iterate(hql,values));
        }

}
